package com.wlyuan.open.application.listener;

import com.wlyuan.core.domain.event.DomainEventListener;
import com.wlyuan.open.OpenBaseSyncDTO;
import com.wlyuan.open.domain.source.SourceRelation;
import com.wlyuan.open.domain.source.dispatcher.OpenSyncDispatcher;
import com.wlyuan.open.domain.source.event.SourceReceived;
import com.wlyuan.open.dto.*;
import com.wlyuan.open.dto.tenant.OpenCompanyTenantSyncDTO;
import com.wlyuan.open.dto.tenant.OpenDriverTenantSyncDTO;
import com.wlyuan.open.dto.tenant.OpenTenantRelationSyncDTO;
import com.wlyuan.open.dto.top.OpenTopExtensionOrderSyncDTO;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import lombok.var;
import org.greenrobot.eventbus.Subscribe;
import org.springframework.stereotype.Service;

/**
 * @author yuanzheng
 */
@Service
@RequiredArgsConstructor
@Slf4j
public class SourceReceivedEventListener implements DomainEventListener<SourceReceived> {
    private final OpenSyncDispatcher openSyncDispatcher;

    private <T extends OpenBaseSyncDTO> void sync(T source, SourceRelation relation) {
        if (source instanceof OpenCustomerSyncDTO) {
            var customer = (OpenCustomerSyncDTO) source;
            customer.setCustomerId(relation.getTargetId());
            openSyncDispatcher.sync(customer);
        } else if (source instanceof OpenConsigneeSyncDTO) {
            var consignee = (OpenConsigneeSyncDTO) source;
            consignee.setConsigneeId(relation.getTargetId());
            openSyncDispatcher.sync(consignee);
        } else if (source instanceof OpenCarrierSyncDTO) {
            var carrier = (OpenCarrierSyncDTO) source;
            carrier.setCarrierId(relation.getTargetId());
            openSyncDispatcher.sync(carrier);
        } else if (source instanceof OpenGoodsSyncDTO) {
            var goods = (OpenGoodsSyncDTO) source;
            goods.setGoodsId(relation.getTargetId());
            openSyncDispatcher.sync(goods);
        } else if (source instanceof OpenUserSyncDTO) {
            var user = (OpenUserSyncDTO) source;
            user.setUserId(relation.getTargetId());
            openSyncDispatcher.sync(user);
        } else if (source instanceof OpenCompanyTenantSyncDTO) {
            var tenant = (OpenCompanyTenantSyncDTO) source;
            tenant.setTenantId(relation.getTargetId());
            openSyncDispatcher.sync(tenant);
        } else if (source instanceof OpenDriverTenantSyncDTO) {
            var tenant = (OpenDriverTenantSyncDTO) source;
            tenant.setTenantId(relation.getTargetId());
            openSyncDispatcher.sync(tenant);
        } else if (source instanceof OpenTransportSyncDTO) {
            var transport = (OpenTransportSyncDTO) source;
            transport.setWaybillId(relation.getTargetId());
            openSyncDispatcher.sync(transport);
        } else if (source instanceof OpenOrderSyncDTO) {
            var order = (OpenOrderSyncDTO) source;
            order.setOrderId(relation.getTargetId());
            openSyncDispatcher.sync(order);
        } else if (source instanceof OpenVehicleSyncDTO) {
            var vehicle = (OpenVehicleSyncDTO) source;
            vehicle.setVehicleId(relation.getTargetId());
            openSyncDispatcher.sync(vehicle);
        } else if (source instanceof OpenDriverSyncDTO) {
            var vehicle = (OpenDriverSyncDTO) source;
            vehicle.setDriverId(relation.getTargetId());
            openSyncDispatcher.sync(vehicle);
        } else if (source instanceof OpenContractSyncDTO) {
            var contract = (OpenContractSyncDTO) source;
            contract.setContractId(relation.getTargetId());
            openSyncDispatcher.sync(contract);
        } else if (source instanceof OpenBillSyncDTO) {
            var finance = (OpenBillSyncDTO) source;
            finance.setBillId(relation.getTargetId());
            openSyncDispatcher.sync(finance);
        } else if (source instanceof OpenTenantRelationSyncDTO) {
            var tenantRelation = (OpenTenantRelationSyncDTO) source;
            tenantRelation.setTenantId(relation.getTargetId());
            openSyncDispatcher.sync(tenantRelation);
        } else if (source instanceof OpenCombineSyncDTO) {
            var combine = (OpenCombineSyncDTO) source;
            combine.setCombineId(relation.getTargetId());
            openSyncDispatcher.sync(combine);
        } else if (source instanceof OpenQuotationSyncDTO) {
            var quotation = (OpenQuotationSyncDTO) source;
            quotation.setQuotationId(relation.getTargetId());
            openSyncDispatcher.sync(quotation);
        } else if (source instanceof OpenTemplateSyncDTO) {
            var template = (OpenTemplateSyncDTO) source;
            template.setTemplateId(relation.getTargetId());
            openSyncDispatcher.sync(template);
        } else if (source instanceof OpenPaySyncDTO) {
            var template = (OpenPaySyncDTO) source;
            template.setPaymentId(relation.getTargetId());
            openSyncDispatcher.sync(template);
        } else if (source instanceof OpenRelationSyncDTO) {
            var rel = (OpenRelationSyncDTO) source;
            rel.setRelationId(relation.getTargetId());
            openSyncDispatcher.sync(rel);
        } else if (source instanceof OpenTopExtensionOrderSyncDTO) {
            var rel = (OpenTopExtensionOrderSyncDTO) source;
            rel.setOrderInfoId(relation.getTargetId());
            openSyncDispatcher.sync(rel);
        } else if (source instanceof OpenCombineQuotationSyncDTO) {
            var rel = (OpenCombineQuotationSyncDTO) source;
            rel.setQuotationId(relation.getTargetId());
            openSyncDispatcher.sync(rel);
        }
    }

    @Override
    @Subscribe
    public void handle(SourceReceived event) {
        if (null != event.getRelation() && event.getRelation().getTargetId() != null) {
            logger.info("处理同步事件: {}", event);
            sync(event.getSource(), event.getRelation());
        }
    }
}
